package com.own.sign.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class WebUtils {

    /**
     * 将字符内容输出页面
     * 
     * @param content
     * @param response
     */
    public static void writeContent(String content, HttpServletResponse response) {
        try {
            response.setHeader("Content-Type", "text/plain;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(content);
            response.getWriter().close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将字符内容输出页面
     * 
     * @param content
     * @param response
     */
    public static void writeContentJson(String content, HttpServletResponse response) {
        try {
            response.setHeader("Content-Type", "text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(content);
            response.getWriter().close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据参数retype决定页面输出的数据,如JSON
     * 
     * @param code
     *            状态码
     * @param msg
     *            状态说明
     * @param data
     *            数据
     * @param request
     * @param response
     */
    public static void doAction(int code, String msg, Object data, HttpServletRequest request,
                                HttpServletResponse response) {

        String retype = request.getParameter("retype");
        JSONObject json = new JSONObject();
        json.put("code", code);// 负数：异常，0：无数据，1：成功
        if (StringUtils.isBlank(msg)) {
            if (code <= 0) {
                msg = "异常";
            }
            else {
                msg = "";
            }
        }
        json.put("msg", msg);
        String resIdx = request.getParameter("resIdx");// 保留标识符
        if (StringUtils.isNotBlank(resIdx)) {
            json.put("resIdx", resIdx);
        }
        if (data != null) {
            json.put("data", JSON.toJSON(data));
        }
        String callback = request.getParameter("callback");
        String result = json.toString();
        if ("jsonp".equals(retype) && StringUtils.isNotBlank(callback)) {// 只有jsonp格式会返回result
            result = callback + "(" + result + ");";
        }
        writeContentJson(result, response);
    }

    public static void doAction(int code, String msg, HttpServletRequest request, HttpServletResponse response) {
        doAction(code, msg, null, request, response);
    }

    /**
     * 输出json格式响应
     * @param code  手机客户端API接口的错误码
     * @param request   请求
     * @param response  响应
     */
    public static void doAction(Enum code, HttpServletRequest request, HttpServletResponse response) {

        Class<? extends Enum > tClass = code.getClass();

        Integer getCode = null;
        String getMsg = null;
        try{
            getCode = (Integer) tClass.getMethod("getCode").invoke(code);
            getMsg = (String) tClass.getMethod("getMsg").invoke(code);
        }catch (Exception e){
            e.printStackTrace();
        }

        doAction(getCode.intValue(), getMsg, null, request, response);
    }

    /**
     * 输出json格式响应
     * @param code  手机客户端API接口的错误码
     * @param data   数据
     * @param request   请求
     * @param response  响应
     */
    public static void doAction(Enum code, Object data, HttpServletRequest request, HttpServletResponse response) {
        Class<? extends Enum > tClass = code.getClass();
        Integer getCode = null;
        String getMsg = null;
        try{
            getCode = (Integer) tClass.getMethod("getCode").invoke(code);
            getMsg = (String) tClass.getMethod("getMsg").invoke(code);
        }catch (Exception e){
            e.printStackTrace();
        }
        doAction(getCode.intValue(), getMsg, data, request, response);
    }

    /**
     * 输出错误信息
     * @param msg
     * @param request
     * @param response
     */
    public static void doError(String msg, HttpServletRequest request, HttpServletResponse response) {
        doAction(-1, msg, null, request, response);
    }

    /**
     * 输出成功信息
     * @param msg
     * @param request
     * @param response
     */
    public static void doSuccess(String msg, HttpServletRequest request, HttpServletResponse response) {
        doAction(1, msg, null, request, response);
    }

    /**
     * 输出成功信息
     * @param msg
     * @param data
     * @param request
     * @param response
     */
    public static void doSuccess(String msg, Object data, HttpServletRequest request, HttpServletResponse response) {
        doAction(1, msg, data, request, response);
    }
}
